﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LandSpikeController : MonoBehaviour {

    private Transform m_transform;
    private Transform m_child_transform;

    private Vector3 normal_position;
    private Vector3 target_position;

    void Start()
    {
        m_transform = gameObject.GetComponent<Transform>();
        m_child_transform = m_transform.Find("moving_spikes_b").GetComponent<Transform>();

        normal_position = m_child_transform.position;
        target_position = m_child_transform.position + new Vector3(0, 0.15f, 0);

        StartCoroutine("StartLandSpikeAnimation");
    }

    public void StartAnimation()
    {
        StartCoroutine("StartLandSpikeAnimation");
    }

    public void StopAnimation()
    {
        StopCoroutine("StartLandSpikeAnimation");
    }
	
    private IEnumerator MoveUp()
    {
        while (true)
        {
            m_child_transform.position = Vector3.Lerp(m_child_transform.position, target_position, Time.deltaTime * 10);
            yield return null;
        }
    }	

    private IEnumerator MoveDown()
    {
        while (true)
        {
            m_child_transform.position = Vector3.Lerp(m_child_transform.position, normal_position, Time.deltaTime * 10);
            yield return null;
        }
    }

    public IEnumerator StartLandSpikeAnimation()
    {
        while (true)
        {
            StartCoroutine("MoveUp");
            yield return new WaitForSeconds(2f);
            StopCoroutine("MoveUp");
            StartCoroutine("MoveDown");
            yield return new WaitForSeconds(2f);
            StopCoroutine("MoveDown");
        }
    }
}
